﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok.Presentation
{
	/// <summary>
	/// </summary>
	internal sealed class ViewDispatcher
	{
		// data
		private readonly HandlerEntry[] mHandlerEntries;


		#region ++ Public Interface ++

		/// <summary>
		/// </summary>
		/// <param name="handlerInfos"></param>
		/// <param name="target"></param>
		public ViewDispatcher(HandlerInfo[] handlerInfos, Object target)
		{
			Verify.NotNull(handlerInfos, "handlerInfos");
			Verify.NotNull(target, "target");

			mHandlerEntries = _BuildHandlerEntries(handlerInfos, target);
		}


		/// <summary>
		/// </summary>
		/// <param name="viewAction"></param>
		/// <returns></returns>
		public Boolean Invoke(ViewAction viewAction)
		{
			if (viewAction == null)
				return false;

			for (Int32 i = 0; i < mHandlerEntries.Length; i++)
			{
				var entry = mHandlerEntries[i];

				if (entry.Match(viewAction))
				{
					var handled = entry.Invoke(viewAction);

					if (handled)
						return true;
				}
			}

			return false;
		}

		#endregion

		#region Implementation

		private HandlerEntry[] _BuildHandlerEntries(HandlerInfo[] handlerInfos, Object target)
		{
			var entries = new HandlerEntry[handlerInfos.Length];

			for (Int32 i = 0; i < handlerInfos.Length; i++)
			{
				entries[i] = new HandlerEntry(
					handlerInfos[i].ViewActionType,
					handlerInfos[i].CreateHandler(target));
			}

			return entries;
		}

		#endregion

		#region Implementation (Nested Types)

		private struct HandlerEntry
		{
			private readonly Type mType;
			private readonly IHandler mHandler;


			public HandlerEntry(Type type, IHandler handler)
			{
				mType = type;
				mHandler = handler;
			}

			public Boolean Match(ViewAction viewAction)
			{
				return mType.IsInstanceOfType(viewAction);
			}

			public Boolean Invoke(ViewAction viewAction)
			{
				return mHandler.Invoke(viewAction);
			}
		}

		#endregion

	}
}
